;
(function() {
    var krpanoSWFObject = null;
    var workPicSwiper = null;
    var photoViewer = null;
    var photoViewer2 = null;
    var sceneClickOnce = false;
    var focusSearch = false;
    var videoPlayer = null;
    var currentPage = 1;
    var doingSearch = false;
    var isPlayedMusic = false;
    var _this = cafaCM = {
        /**
         * 全景应用初始化
         * @param {integer} index 场景索引值
         */
        initPano: function(index, xml) {
            if (!xml) {
                return;
            }
            embedpano({
                xml: xml,
                target: 'pano',
                html5: "auto",
                mobilescale: 1.0,
                passQueryParameters: true,
                onready: function(pano) {
                    krpanoSWFObject = pano;
                    if (index) {
                        krpanoSWFObject.call('loadscene("scene_' + index + '");');
                    }
                }
            });
        },
        /**
         * 获取全景配置
         */
        getShowroomInfo: function() {
            if (!window.util) {
                return;
            }
            // 获取guid
            var guid = util.getQueryString('guid');
            if (!guid) {
                return;
            }
            ajax.getShowroomInfo({
                guid: guid,
                success_cb: function(res) {
                    var data = res.data;
                    _this.initPano('', data.pano_xml);
                    _this.initPanoScene();
                    _this.initVRMode();
                    _this.initMusic(data.music);
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            })
        },
        /**
         * 获取学生作品详情
         * @param {integer} student_no 学号
         */
        getStudentWorkDetail: function(student_no, guid) {
            if (!student_no) {
                return;
            }
            if (guid) {
                window.location.href = util.setHashString('student_no', student_no, util.getNewRequestUrl([{ key: 'guid', val: guid }]));
            }
            _this.storeSceneParams('student_no', student_no);
            ajax.getWorkList({
                student_no: student_no,
                success_cb: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    var detail = res.data[0];
                    _this.getStudentInfo(student_no, function(studentInfo) {
                        detail.studentInfo = studentInfo;
                        _this.showWorkModal(detail);
                        $('#workModal .intro').scrollTop(0);
                        if (window.util && !util.isMobile()) {
                            $('#workDetail .intro').getNiceScroll().resize(); // 滚动条重新更新
                        }
                        _this.updateWorkView(detail.id);
                    });
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            });
        },
        /**
         * 更新作品浏览数
         * @param id int 作品id
         */
        updateWorkView: function(id) {
            if (!id) {
                return;
            }

            ajax.updateWorkView({
                work_id: id,
                success_cb: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            });
        },
        /**
         * 设置作品cookie
         * @param options object 参数对象
         */
        // setWorkCookie: function(options) {
        //     var defaultOptions = {
        //         id: 0,
        //         is_like: 0,
        //         is_view: 0,
        //         view_date: 0
        //     }
        //     var workCookie = _this.getWorkCookie(options.id);
        //     if (workCookie) {
        //         workCookie = JSON.parse(workCookie);
        //         defaultOptions = workCookie;
        //     }
        //     var opts = $.extend(defaultOptions, options);
        //     if (!opts.id) {
        //         return;
        //     }

        //     $.cookie('work_' + opts.id, JSON.stringify({
        //         id: opts.id,
        //         is_like: opts.is_like,
        //         is_view: opts.is_view,
        //         view_date: opts.view_date
        //     }), opts.expires ? opts.expires : 30);
        // },
        /**
         * 获取学生信息
         * @param studentNo int 学生学号
         */
        getStudentInfo: function(studentNo, success_cb) {
            if (!studentNo) {
                return;
            }
            ajax.getStudentInfo({
                student_no: studentNo,
                success_cb: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    if (success_cb && typeof success_cb === 'function') {
                        success_cb(res.data);
                    }
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            })
        },
        /**
         * 显示作品弹窗
         */
        showWorkModal: function(data) {
            if (!data) {
                return;
            }

            _this.modifyModalData(data);
            $('#workModal').show().attr('data-id', data.id);
            $('#workModal .modal-bd').scrollTop(0);
            $('#workModal .intro').scrollTop(0);



            if (workPicSwiper) {
                workPicSwiper.destroy(true, true);
            }

            _this.initWorkPicSwiper();
            _this.initPhotoViewer2();
            $('#contModel').on('click', function(e) {
                e.stopPropagation();
                _this.openModelModal();
            });
            $('#contVideo').on('click', function(e) {
                e.stopPropagation();
                _this.showVideo($('#contVideo').attr('data-url'));
                _this.openVideoModal();
            });

            $('.top-video-img').on('click', function(e) {
                e.stopPropagation();
                _this.showVideo(data['top_video_resource'][0]['ts']);
                _this.openVideoModal();
            });
            if (window.util && !util.isMobile()) {
                $('#workDetail').getNiceScroll().resize(); // 滚动条重新更新
            }
        },
        /**
         * 初始化作品图片列表轮播
         */
        initWorkPicSwiper: function() {
            workPicSwiper = new Swiper('#workPicList', {
                loop: false,
                onInit: function(workPicSwiper) {
                    var total = workPicSwiper.slides.length;
                    $('#workPicList #total').html('/' + total);
                    $('#workPicList #curr').html('1');
                },
                onSlideChangeEnd: function(workPicSwiper) {
                    $('#workPicList #curr').html(workPicSwiper.realIndex + 1);
                }
            })

            $('#workPicList .btn-prev').off('click');
            $('#workPicList .btn-next').off('click');
            $('#workPicList .btn-prev').on('click', function(e) {
                e.stopPropagation();
                workPicSwiper.slidePrev();
            });

            $('#workPicList .btn-next').on('click', function(e) {
                e.stopPropagation();
                workPicSwiper.slideNext();
            });


            _this.initPhotoViewer();
        },
        showVideo: function(url) {
            $('#videoBox').attr('data-src', url);
        },
        /**
         * 修改热点弹窗内容
         */
        modifyModalData: function(data) {

            studentInfo = data.studentInfo;
            if (!data) {
                return;
            }
            $('.intro dl').hide();
            $('#contModel').hide();
            $('#contVideo').hide();
            $('#workModal .btn-like').removeClass('on');

            var assetsPic = data['pic_resource'];
            var assetsTopVideo = data['top_video_resource'];
            var assetsVideo = data['video_resource'];
            var assets3D = data['3d_resource'];
            var assets360 = data['360_resource'];
            var html = '';
            if (assetsTopVideo.length) {
                $('#workModal').attr('data-thumb', data.top_video_resource[0].cover);
            } else {
                $('#workModal').attr('data-thumb', data.thumb);
            }
            if (assetsPic.length == 0 && assetsTopVideo == 0) {
                $('#workModal .swiper-container').hide();
            } else {
                if (studentInfo.is_design) {
                    for (var i = 0; i < assetsTopVideo.length; i++) {
                        html += '<div class="top-video-img" style="background-image: url(' + assetsTopVideo[i].cover + ');">';
                        html += '<div class="btn"></div>';
                        html += '</div>';
                    }
                    $('#workModal .swiper').hide();
                    $('#workModal .top-video').html(html);
                    $('#workModal .top-video').show();
                } else {
                    for (var i = 0; i < assetsPic.length; i++) {
                        html += '<li class="swiper-slide">';
                        html += '   <div class="img" style="background-image: url(' + assetsPic[i].thumb + ');">';
                        html += '       <img src="' + assetsPic[i].thumb + '" data-original="' + assetsPic[i].url + '">';
                        html += '   </div>';
                        html += '</li>';
                    }
                    $('#workModal .top-video').hide();
                    $('#workPicList .swiper-wrapper').html(html);
                    $('#workModal .swiper-container').show();
                    $('#workModal .swiper').show();

                }
            }

            if (data.series_name) {
                $('#seriesName dd').html(data.series_name);
                $('#seriesName').show();
            }
            if (data.name) {
                $('#name dd').html(data.name);
                $('#name').show();
            }
            if (studentInfo) {
                $('#student dd').html(studentInfo.name + '&emsp;|&emsp;' + studentInfo.student_no);
                $('#student').show();
            }
            var hasCollege = false;
            if (studentInfo.college_name || studentInfo.department_name) {
                $('#departmentName dd').html('');
                if (studentInfo.college_name) {
                    hasCollege = true;
                    $('#departmentName dd').append(studentInfo.college_name);
                    $('#departmentName').attr('data-college', studentInfo.college_no)
                }
                if (studentInfo.department_name) {
                    if (hasCollege) {
                        $('#departmentName dd').append('&emsp;|&emsp;');
                    }
                    $('#departmentName dd').append(studentInfo.department_name);
                }
                $('#departmentName').show();
            }
            if (data.teacher) {
                $('#teacher dd').html(data.teacher);
                $('#teacher').show();
            }
            if (data.direction) {
                $('#direction dd').html(data.direction);
                $('#direction').show();
            }
            if (data.size) {
                $('#size dd').html(data.size);
                $('#size').show();
            }
            if (data.texture) {
                $('#texture dd').html(data.texture);
                $('#texture').show();
            }
            if (data.description) {
                $('#description dd').html(data.description);
                $('#description').show();
            }
            if (data.content) {
                $('#content dd').html(data.content);
                $('#content').show();

            }
            if (assets3D) {
                //$('#contModel img').attr('src', data.thumb);
                $('#modelModal iframe').attr('data-src', assets3D);
                $('#contModel').attr('style', 'display:block');
            }
            if (assetsVideo.length > 0) {
                $('#contVideo').html('<img src="' + data.video_resource[0].cover + '"/>');
                $('#contVideo').attr('style', 'display:block');
                $('#contVideo').attr('data-url', data.video_resource[0].url);
            }
            $('#workModal .btn-like em').text(data.good_num);
            $('.wx-account img').attr('src', data.college_image);
            if (data.is_good === 1) {
                $('#workModal .btn-like').addClass('on');
            }
            $('#workModal .btn-like em').text(data.good_num);
            if (window.pattern && window.pattern == '2d' && (studentInfo.is_design || (studentInfo.pa_id && studentInfo.pa_id != '0'))) {
                $('#workModal .btn-360').off('click');
                $('#workModal .btn-360').show().on('click', function(e) {
                    e.stopPropagation();
                    _this.goToPano(studentInfo.student_no);
                });
            } else {
                $('#workModal .btn-360').hide();
            }
            if (util && !util.isMobile()) {
                $('#workModal .mask').on('click', function(e) {
                    //e.stopPropagation();
                    $('#workModal').hide();
                });
            }
        },
        /**
         * 获取作品cookie
         * @param id int 作品id
         */
        // getWorkCookie: function(id) {
        //     if (!id) {
        //         return;
        //     }
        //     return $.cookie('work_' + id);
        // },
        /**
         * 初始化查看大图弹框
         */
        initPhotoViewer: function() {
            if (photoViewer) {
                photoViewer.destroy();
            }
            photoViewer = new Viewer($('#workPicList')[0], {
                url: 'data-original',
                title: false,
                navbar: false,
                toolbar: {
                    zoomIn: false,
                    zoomOut: false,
                    oneToOne: false,
                    reset: false,
                    prev: util.isMobile() ? false : true,
                    next: util.isMobile() ? false : true,
                    rotateLeft: false,
                    rotateRight: false,
                    flipHorizontal: false,
                    flipVertical: false
                },
                ready: function() {
                    if (util.isMobile()) {
                        $('.viewer-container').css('zoom', 1 / $('body').css('zoom'));
                    }
                }
            });
        },
        initPhotoViewer2: function() {
            if (photoViewer2) {
                photoViewer2.destroy();
            }
            photoViewer2 = new Viewer($('#workDetail')[0], {
                url: 'data-src',
                title: false,
                navbar: false,
                toolbar: {
                    zoomIn: false,
                    zoomOut: false,
                    oneToOne: false,
                    reset: false,
                    prev: util.isMobile() ? false : true,
                    next: util.isMobile() ? false : true,
                    rotateLeft: false,
                    rotateRight: false,
                    flipHorizontal: false,
                    flipVertical: false
                },
                ready: function() {
                    if (util.isMobile()) {
                        $('.viewer-container').css('zoom', 1 / $('body').css('zoom'));
                    }
                }
            });
        },
        /**
         * 弹窗初始化
         */
        initModal: function() {
            var zoom = parseFloat($('body').css('zoom'));
            if (zoom) {
                $('#videoBox').css('zoom', 1 / zoom);
            }

            var student_no = util.getQueryString('student_no');
            $('#workModal .btn-close').off('click');
            if (window.location.href.indexOf('/student/info') > -1) {
                if (util.getQueryString('source_from') == '2d') {

                    _this.getStudentInfo(student_no, function(studentInfo) {
                        var college_no = studentInfo.department_no && studentInfo.department_no != '0' ? studentInfo.department_no : studentInfo.college_no;
                        var url = window.location.protocol + '//' + window.location.host;
                        if (window.location.href.indexOf('school_new') > 0 || window.location.href.indexOf('student/info') > 0) {
                            url += '/school_new'
                        }
                        url += '/cloud'
                        if (window.util && util.isMobile()) {
                            url += '/m/';
                        } else {
                            url += '/';
                        }
                        url += 'list.html?college_no=' + college_no;

                        $('#workModal .btn-close').on('click', function(e) {
                            e.stopPropagation();
                            window.location.href = url;
                        });

                    });
                } else {
                    $('#workModal .btn-close').on('click', function(e) {
                        e.stopPropagation();
                        _this.goToPano(util.getQueryString('student_no'));
                    });
                }
            } else {
                $('#workModal .btn-close').on('click', function(e) {
                    e.stopPropagation();
                    $('#workModal').hide();
                });
            }

            $('.full-modal .btn-close').on('click', function(e) {
                e.stopPropagation();
                var $videoBox = $(this).siblings('#videoBox');
                $(this).closest('.full-modal').hide();
                if ($videoBox.length) {
                    videoPlayer.pause();
                }
            });
            if (window.util && !util.isMobile()) {
                $(document).on('ready', function() {
                    $('#workDetail').niceScroll({
                        cursorborder: 'none',
                        cursorcolor: 'rgba(0, 0, 0, 0.4)',
                    });
                });
            }

            // 移动端监听横竖屏
            if (window.util && util.isMobile()) {
                $(window).on('orientationchange', function() {
                    if (workPicSwiper) {
                        workPicSwiper.update(true);
                    }
                });
            }
        },
        /**
         * 初始化作品点赞功能
         */
        initWorkLike: function() {

            var $btnLike = $('#workModal .btn-like');
            $btnLike.on('click', function(e) {
                e.stopPropagation();
                var $workId = $('#workModal').attr('data-id');
                _this.handleWorkLike($workId);
            })
        },
        /**
         * 作品点赞
         * @param id int 作品id
         */
        handleWorkLike: function(id) {
            if (!id) {
                return;
            }

            // var workCookie = $.cookie('student_work_like');
            // var arr = [];
            // if (workCookie) {
            //     workCookie = JSON.parse(workCookie);
            //     arr = workCookie.id;
            //     if ($.inArray(id, arr) >= 0) {
            //         alert('每个作品每天最多只可点赞1次');
            //         return;
            //     }
            //     if (arr.length >= 10) {
            //         alert('每天最多只可点赞10个作品');
            //         return;
            //     }
            // }
            // arr.push(id);
            ajax.handleWorkLike({
                work_id: id,
                success_cb: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        return;
                    }
                    $('#workModal .btn-like').addClass('on');
                    $('#workModal .btn-like em').text(res.data.good_num);
                    if (data.is_good === 1) {
                        $('#workModal .btn-like').addClass('on');
                    }
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            });

        },
        /**
         * 打开3d模型弹窗
         */
        openModelModal: function() {
            var url = $('#modelModal iframe').attr('data-src');
            $('#modelModal iframe').attr('src', url);
            $('#modelModal').show();
        },
        /**
         * 打开视频弹窗
         */
        openVideoModal: function() {
            var url = $('#videoBox').attr('data-src');
            if (!url) {
                return;
            }

            if (!$('#videoBox video').length) {
                var html = '<video ';
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent) && util.getMobileBrowser() === 'MQQBrowser') { // ios端的QQ浏览器
                    html += 'style="width: 100%; height: 100%;" ';
                }
                if (util.isMobile()) {
                    html += 'playsinline webkit-playsinline x5-playsinline';
                }
                html += 'controls autoplay data-setup="{}" preload="auto" class="video-js" id="videoPlayer">';
                if (url.indexOf('.m3u8') > -1) {
                    html += '<source src="' + url + '" type="application/x-mpegURL"></source>';
                } else {
                    html += '<source src="' + url + '" type="video/mp4"></source>';
                }
                html += '</video>';
                $('#videoBox').html(html);
            }

            if (!videoPlayer) {
                videoPlayer = videojs('videoPlayer', {
                    controls: true,
                    nativeControlsForTouch: false
                }, function() {
                    this.play();
                });
            } else {
                videoPlayer.src(url);
                videoPlayer.currentTime(0);
                videoPlayer.play();
            }

            $('#videoModal').show();
        },
        /**
         * 初始化前言弹窗
         */
        initPreface: function() {
            // $('#collegePreface img').on('click', function(e) {
            //     e.stopPropagation();
            //     $(this).hide();
            //     $('#collegePreface .cont').show();
            // });
            // $('#collegePreface .cont').on('click', function(e) {
            //     e.stopPropagation();
            // });

            if (!util.isMobile()) {
                $('#floorPreface .word-main').niceScroll({
                    cursorborder: 'none',
                    cursorcolor: 'rgba(0, 0, 0, 0.4)'
                });
                $('#collegePreface .bd').niceScroll({
                    cursorborder: 'none',
                    cursorcolor: 'rgba(0, 0, 0, 0.4)'
                });
            }

            // 学院前言导航点击
            $('#collegePreface .hd a').on('click', function(e) {
                e.stopPropagation();
                var index = $(this).index();
                var $cont = $('#collegePreface .cont');

                $(this).addClass('on').siblings().removeClass('on');
                $cont.removeClass('on');
                $cont.eq(index).addClass('on');
                $('#collegePreface .bd').scrollTop(0);

                if (!util.isMobile()) {
                    $('#collegePreface .bd').getNiceScroll().resize();
                }
            });

            $('.college-preface .btn-close').on('click', function() {
                $('.college-preface').hide();
                // 销毁设计学院播放器
                if (typeof(design_player_1) != "undefined" && design_player_1 && design_player_1.dispose) {
                    design_player_1.dispose();
                    design_player_1 = null
                }
            });

            $('.college-preface .img-wrap').on('click', function(e) {
                e.stopPropagation();
                $('.college-preface .img-wrap').hide();
                $('#collegePreface .bd').scrollTop(0);
                if (!util.isMobile()) {
                    $('#collegePreface .bd').getNiceScroll().resize();
                }
                $('.college-preface .pop-main').show();

            })
        },
        // /**
        //  * 初始化学院导航
        //  */
        // initCollegeNav: function() {
        //     $('#btnSwitch').on('click', function(e) {
        //         e.stopPropagation();
        //         $('#collegeNav').addClass('on');
        //     });
        //     $('#collegeNav a').on('click', function() {
        //         $(this).addClass('on').siblings().removeClass('on');
        //         $('#collegeNav').removeClass('on');
        //     });
        //     $('#collegeNav').on('click', function(e) {
        //         e.stopPropagation();
        //     });
        // },
        /**
         * 全景界面点击
         */
        sceneClick: function() {
            $('#collegeNav').removeClass('on');
            if (!sceneClickOnce && $('#bgMusic').length && $('#bgMusic')[0].paused) {
                _this.playMusic();
                sceneClickOnce = true;
            }
        },
        /**
         * 初始化VR模式
         */
        initVRMode: function() {
            $('#btnVR').on('click', function() {
                if (window.util && util.isMobile() && $(this).hasClass('on')) {
                    $(this).removeClass('on');
                    if (krpanoSWFObject) {
                        krpanoSWFObject.call('webvr.exitVR()');
                    }
                } else {
                    $(this).addClass('on');
                    if (krpanoSWFObject) {
                        krpanoSWFObject.call('webvr.enterVR()');
                    }
                }

            });
        },
        /**
         * 初始化背景音乐
         * @param {object} options 背景音乐配置信息
         */
        initMusic: function(options) {
            var $btnMusic = $('#btnMusic');
            if (!options) {
                $btnMusic.hide();
                return;
            }

            $btnMusic.removeClass('on');
            $btnMusic.css('display', 'inline-block');
            var html = '';
            html += '<div style="width: 0;height: 0;overflow:hidden;"><audio id="bgMusic" preload src="' + options.url + '"';
            html += options.isLoop ? ' loop' : '';
            html += '/></div>';
            $('body').append(html);
            $('#bgMusic')[0].autoplay = true;
            if (options.volume !== undefined) {
                $('#bgMusic')[0].volume = options.volume;
            }

            if ($('#bgMusic')[0].paused) {
                _this.playMusic();
            }

            if (window.wx) {
                document.addEventListener("WeixinJSBridgeReady", function() {
                    _this.playMusic();
                }, false);
            } else {
                $('body').one('click', function() {
                    if (!isPlayedMusic && $('#bgMusic')[0].paused) { // 是否播放过音乐
                        _this.playMusic();
                    }
                });
            }
            if (options.volume !== undefined && options.volume === 0) {
                _this.pauseMusic();
            }
            $btnMusic.on('click', function(e) {
                e.stopPropagation();
                if ($('#bgMusic')[0].paused) {
                    _this.playMusic();
                } else {
                    _this.pauseMusic();
                }
            });
        },
        /**
         * 播放背景音乐
         */
        playMusic: function() {
            isPlayedMusic = true;
            $('#bgMusic')[0].play();
            if (!$('#bgMusic')[0].paused) {
                $('#btnMusic').removeClass('off').addClass('on');
            }
        },

        /**
         * 关闭背景音乐
         */
        pauseMusic: function() {
            isPlayedMusic = true;
            $('#bgMusic')[0].pause();
            $('#btnMusic').removeClass('on').addClass('off');
        },
        /**
         * 初始化搜索
         */
        initSearch: function() {
            // $('#searchBox input').on('focus', function() {
            //     $('#searchBox').addClass('on');
            // });
            if (window.util && util.isMobile()) {
                $('#btnSearch').on('click', function() {
                    $('#pageSearch').show();
                    $('#searchList').html('');
                    $('#searchBox input').val('');
                });

                $('#pageSearch .btn-cancel').on('click', function() {
                    $('#pageSearch').hide();
                    $('#searchList').html('');
                    $('#searchBox input').val('');
                });
            } else {
                $(document).on('ready', function() {
                    $('#searchList').niceScroll({
                        cursorborder: 'none',
                        cursorcolor: 'rgba(0, 0, 0, 0.4)'
                    });
                });
            }
            $('#searchBox input').on('focus', function() {
                //e.stopPropagation();
                //currentPage = 1;
                //$('#searchList').html('');
                //$('#searchList').scrollTop(0);
                //$('#searchBox .more').html('无搜索结果').show();
                focusSearch = true;
                _this.doSearch();
            });

            $('#searchBox input').on('click', function(e) {
                e.stopPropagation();
            });

            $('#searchBox input').on('input', function(e) {
                e.stopPropagation();
                currentPage = 1;
                $('#searchList').html('');
                $('#searchList').scrollTop(0);
                if (window.util && !util.isMobile()) {
                    $('#searchList').getNiceScroll().resize();
                }
                _this.doSearch();
            });
            $('#searchList .more').live('click', function(e) {
                $(this).remove();
                e.stopPropagation();
                _this.doSearch();
            });

        },
        getSearchHtml: function(data, keyword) {
            var html = '';
            for (var i = 0; i < data.length; i++) {
                var reg = new RegExp("(" + keyword + ")", "g");
                //var str = data[i].student_name + '&nbsp;&nbsp;' + data[i].student_no;
                var str = data[i].map_value;
                var newstr = str.replace(reg, "<em>$1</em>");
                html += '<li>';
                html += '<a onclick="cafaCM.handleSearchResult(\'' + data[i].student_no + '\', ' + data[i].is_design + ')">' + newstr + '</a>';
                html += '</li>';
            }
            return html;
        },
        /**
         * 执行搜索
         */
        doSearch: function() {
            if (doingSearch) {
                return;
            }
            var keyword = $('#searchBox input').val();
            if (!keyword) {
                $('#searchBox').removeClass('on');
                $('#searchList').html('');
                focusSearch = false;
                return;
            }
            if (focusSearch) {
                $('#searchBox').addClass('on');
                focusSearch = false;
                return;
            }
            doingSearch = true;
            $('#searchBox .more').remove();
            focusSearch = false;
            ajax.getSearchInfo({
                keyword: keyword,
                type: 2,
                page: currentPage,
                success_cb: function(res) {
                    if (res.errorcode) {
                        alert(res.message);
                        doingSearch = false;
                        //$('#searchBox .more').show();
                        return;
                    }
                    var data = res.data.list;
                    var html = '';
                    if (currentPage == 1 && data.length == 0) {
                        html += '<li class="no-more"><a href="javascript:;">无搜索结果</a></li>';
                    } else if (data.length == 0) {
                        html += '<li class="no-more"><a href="javascript:;">无更多数据</a></li>';
                    } else {
                        html += _this.getSearchHtml(data, keyword);
                        html += '<li class="more"><a href="javascript:;">点击加载更多数据</a></li>';
                    }
                    $('#searchList').append(html);
                    currentPage++;
                    if (window.util && !util.isMobile()) {
                        $('#searchList').getNiceScroll().resize();
                    }
                    $('#searchBox .more').on('click', function(e) {
                        e.stopPropagation();
                        _this.doSearch();
                    });
                    $('#searchBox').addClass('on');
                    doingSearch = false;

                },
                fail_cb: function(err) {
                    console.error(err);
                }
            });
        },
        /**
         * 跳转360场景
         * @param {integer} studentNo 学号
         */
        goToPano: function(studentNo) {
            ajax.getStudentInfo({
                student_no: studentNo,
                success_cb: function(res) {
                    var data = res.data;
                    var guid = util.getQueryString('guid');
                    var currUrl = window.location.href;

                    var url = window.location.protocol + '//' + window.location.host;
                    if (window.location.href.indexOf('school_new') > 0 || window.location.href.indexOf('student/info') > 0) {
                        url += '/school_new'
                    }

                    if (data.is_design) {
                        if (window.util && util.isMobile()) {
                            url += '/design/m/';
                        } else {
                            url += '/design/';
                        }
                        url += 'index.html#student_no=' + studentNo.toString();
                    } else {
                        if (!data.shr_guid) {
                            alert('该学生暂未绑定场景');
                            return;
                        }
                        if (window.util && util.isMobile()) {
                            url += '/m/';
                        } else {
                            url += '/';
                        }

                        url += 'vtour.html?guid=' + data.shr_guid + '#pa_id=' + data.pa_id + '&student_no=' + data.student_no;
                    }
                    window.location.href = url;
                    if (guid === data.shr_guid) {
                        window.location.reload();
                    }
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            })
        },
        /**
         * 处理搜索结果
         */
        handleSearchResult: function(studentNo, is_design) {
            if (!studentNo) {
                return;
            }

            if (window.pattern && window.pattern === '2d') {
                $('#pageSearch').hide();
                $('#searchBox').removeClass('on');
                _this.getStudentWorkDetail(studentNo);
            } else if (window.pattern && window.pattern === '3d') {
                if (is_design) {

                    if (window.location.href.indexOf('/design/') > -1) {
                        $('#pageSearch').hide();
                        $('#searchBox').removeClass('on');
                        // _this.getStudentWorkDetail(studentNo);
                        aniFn.goToStudentInfo(studentNo)
                    } else {
                        var url = window.location.protocol + '//' + window.location.host;
                        if (window.location.href.indexOf('school_new') > 0 || window.location.href.indexOf('student/info') > 0) {
                            url += '/school_new'
                        }
                        if (window.util && util.isMobile()) {
                            url += '/design/m/';
                        } else {
                            url += '/design/';
                        }
                        // if (currUrl.indexOf('/design/') > -1 || currUrl.indexOf('/cloud/') > -1 || currUrl.indexOf('/student_search/') > -1) {
                        //     url = '../' + url;
                        // }
                        // console.log('student_search=', currUrl.indexOf('/student_search/'))
                        url += 'index.html#student_no=' + studentNo.toString();

                        window.location.href = url;
                    }

                    return;
                }
                _this.goToPano(studentNo);
            }
        },
        /**
         * 保存场景参数
         */
        storeSceneParams: function(name, value, url) {
            if (!window.util) {
                return;
            }
            var url = util.setHashString(name, value, url);
            window.location.href = url;
        },
        /**
         * 初始化全景场景（是否带参数）
         */
        initPanoScene: function() {
            var paId = util.getHashString('pa_id'); // 场景id
            studentNo = util.getHashString('student_no'); // 学生学号

            if (paId) {
                if (krpanoSWFObject) {
                    krpanoSWFObject.call('loadscene("scene_' + paId + '");');
                }
            }

            _this.getStudentInfo(studentNo, function(studentInfo) {
                var hotspotInfo = studentInfo.hotspot;
                if (krpanoSWFObject) {
                    krpanoSWFObject.call('lookat(' + hotspotInfo.hlookat + ', ' + hotspotInfo.vlookat + ', ' + hotspotInfo.fov + ')');
                }
            });
        },
        /**
         * 初始化全页点击事件
         */
        initDocumentEvent: function() {
            $(document).click(function() {
                $('#searchBox').removeClass('on');;
            });
        },
        /**
         * 初始化学院列表信息
         */
        initCollegeList: function(func) {
            // if (sessionStorage.getItem('college')) {
            //     window.collegeList = JSON.parse(sessionStorage.getItem('college')).list;
            //     if (func && typeof func === 'function') {
            //         func(JSON.parse(sessionStorage.getItem('college')).list);
            //     }
            //     return;
            // }
            if (window.collegeList) {
                if (func && typeof func === 'function') {
                    func(window.collegeList);
                }
                return;
            }
            ajax.getCollegeList({
                type: 2,
                success_cb: function(res) {
                    window.collegeList = res.data.list;
                    if (func && typeof func === 'function') {
                        func(res.data.list);
                    }
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            });
        },
        /**
         * 初始化场馆列表信息
         */
        initSceneList: function(func) {
            //var sceneList = JSON.parse(sessionStorage.getItem('sceneList'));
            var sceneList = window.sceneList;
            if (sceneList) {
                if (func && typeof func === 'function') {
                    func(sceneList);
                }
                return;
            }
            ajax.getShowroomList({
                success_cb: function(res) {
                    var sceneList = res.data.list;
                    window.sceneList = sceneList;
                    //sessionStorage.setItem('sceneList', JSON.stringify(sceneList));
                    if (func && typeof func === 'function') {
                        func(sceneList);
                    }
                },
                fail_cb: function(err) {
                    console.log(err);
                }
            });
        },
        /**
         * 获取视频详情
         * @param {integer} videoId 视频id
         */
        getVideoDetail: function(videoId) {
            if (!videoId) {
                return;
            }
            ajax.getVideoDetail({
                video_id: videoId,
                success_cb: function(res) {
                    var url = res.data.video.video;
                    $('#videoBox').attr('data-src', url);
                    _this.openVideoModal();
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            })
        },
        /**
         * 获取学生视频详情
         * @param {integer} studentNo 学生学号
         * @param {integer} videoId 视频id
         */
        getStudentVideoDetail: function(studentNo, videoId) {
            console.log(studentNo, videoId);
            if (!studentNo || !videoId) {
                return;
            }
            ajax.getStudentVideoDetail({
                student_no: studentNo,
                video_id: videoId,
                success_cb: function(res) {
                    var url = res.data.video.video;
                    $('#videoBox').attr('data-src', url);
                    _this.openVideoModal();
                },
                fail_cb: function(err) {
                    console.error(err);
                }
            })
        },
        showCollegePreface: function(college_no) {
            ajax.getCollegeInfo({
                college_no: college_no,
                success_cb: function(data) {
                    var info = data.data.info;
                    var image = info.image.foreword;
                    if (window.util && util.isMobile()) {
                        image = info.image.foreword_m;
                    }
                    $("#collegePreface .img-wrap img").remove();
                    $("#collegePreface .img-wrap").html('<img src="' + image + '">');
                    $("#collegePreface .img-wrap").show();
                    $("#collegePreface .pop-main").hide();
                    $("#collegePreface .foreword .tit").html(info.intro.foreword.title);
                    $("#collegePreface .foreword .intro").html(info.intro.foreword.content);
                    $("#collegePreface .about .tit").html(info.name);
                    $("#collegePreface .about .intro").html(info.intro.about);
                    cafaCM.initPreface();
                    $("#collegePreface").show();
                    return;
                },
                fail_cb: function(err) {
                    console.log(err)
                }
            });
        },
        showCollegeResearchDirection: function(direction_id) {
            ajax.getDirectionInfo({
                direction_id: direction_id,
                success_cb: function(data) {
                    var direction = data.data.direction;

                    $("#collegeResearchDirection .direction_title_1").text(direction.title_1);
                    $("#collegeResearchDirection .direction_content_1").html(direction.content_1);
                    $("#collegeResearchDirection .direction_title_2").text(direction.title_2);
                    $("#collegeResearchDirection .direction_content_2").html(direction.content_2);
                    $("#collegeResearchDirection").show();
                    return;
                },
                fail_cb: function(err) {
                    console.log(err)
                }
            });
        },
        /**
         * 显示隐藏的页面元素（krpano xml调用）
         */
        showPageElements: function() {
            $('.social-op').show();
            $('.header').show();
            $('#btnSwitch').show();
        },
        /**
         * 显示隐藏的页面元素（krpano xml调用）
         */
        hidePageElements: function() {
            $('.social-op').hide();
            $('.header').hide();
            $('#btnSwitch').hide();
        },

        /**
         * 打开链接
         * @param mode
         * @param url
         */
        openUrl: function(mode, url) {
            if(mode == 1){
                window.open(url);
            }else{
                window.location.href = url;
            }
        },

        /**
         * 初始化
         */
        init: function() {
            this.initPreface();
            this.getShowroomInfo();
            this.initCollegeList();
            this.initModal();
            this.initSearch();
            this.initWorkLike();
            this.initDocumentEvent();

            $(window).on('click', function() {
                $('#collegeNav').removeClass('on');
                //$('#workModal').hide();
            });
        },
    };
    window.cafaCM = cafaCM;
    window.showWorkModal = cafaCM.showWorkModal;
    window.getStudentWorkDetail = cafaCM.getStudentWorkDetail;
    window.sceneClick = cafaCM.sceneClick;
    window.storeSceneParams = cafaCM.storeSceneParams;
    window.getVideoDetail = cafaCM.getVideoDetail;
    window.getStudentVideoDetail = cafaCM.getStudentVideoDetail;
    window.showCollegePreface = cafaCM.showCollegePreface;
    window.showCollegeResearchDirection = cafaCM.showCollegeResearchDirection;
    window.showPageElements = cafaCM.showPageElements;
    window.hidePageElements = cafaCM.hidePageElements;
    window.openUrl = cafaCM.openUrl;
})();